import os
import sys
import time

fifoname = r'D:\pipefifo'


def child():
    pipeout = os.open(fifoname, os.O_WRONLY)
    zzz = 0
    while True:
        time.sleep(zzz)  # make parent wait
        msg = ('Spam %03d\n' % zzz).encode()  # pipes are binary in 3.X
        os.write(pipeout, msg)  # send to parent
        zzz = (zzz+1) % 5  # roll to 0 at 5


def parent():
    pipein = os.open(fifoname, 'r')
    while True:
        line = pipein.readline()[:-1]  # blocks until data sent
        print('Parent %d got [%s] at %s' % (os.getpid(), line, time.time()))


if __name__ == '__main__':
    if not os.path.exists(fifoname):
        os.mkfifo(fifoname)
    if len(sys.argv) == 1:
        parent()
    else:
        child()
